﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using ueEngine.Framework.Common;
using ueEngine.Web.WebLogic;

namespace ueEngine.Web
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.Add("staticurl",
                new StaticURLRoute("staticurl",
                    "{controller}/{action}_{id}",
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
                    ));
            //routes.MapRoute(
            //    "staticUrl", // 路由名称
            //    "{controller}/{action}_{id}", // 带有参数的 URL
            //    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            //);

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );
            
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

        }

        protected void Application_Error()
        {
            var exp = Server.GetLastError();
            //LogHelper.Write(CommonLogger.WebError, LogLevel.Error, exp, "Application Error ");
            if (exp is HttpException)
            {
                if ((exp as HttpException).GetHttpCode() == 404)
                {
                    if(Request.AcceptTypes[0] == "text/html")
                    {
                        Response.Redirect("/home/notfound");
                    }
                }
            }
        }

        protected void Session_Start() 
        {
            var url = Request.Url.ToString();
            if (url.IndexOf("cqdxsjz")>0)
            {
                url = url.Replace("cqdxsjz", "yaojianzhi");
                Response.Redirect(url);
            }
        }
    }
}